package top.hymsk.ecommerce.controller.restful.order;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hymsk.ecommerce.anno.Log;
import top.hymsk.ecommerce.anno.TokenAnalysis;
import top.hymsk.ecommerce.pojo.response.GeneralResponse;
import top.hymsk.ecommerce.pojo.status.ResponseStatusCode;
import top.hymsk.ecommerce.service.OrderService;

import javax.servlet.http.HttpServletRequest;

/**
 * 卖家订单 /order/seller
 *
 * @author 花于陌上开
 */
@CrossOrigin  //允许跨域访问
@RestController
@RequestMapping("/order/seller")
public class SellerOrderRestController {
    private final HttpServletRequest request;
    private final OrderService orderService;

    SellerOrderRestController(HttpServletRequest request, OrderService orderService) {
        this.request = request;
        this.orderService = orderService;
    }

    /**
     * 获取订单列表
     *
     * @param token 用户鉴权token
     * @param page 分页参数
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/list")
    public GeneralResponse getOrderList(String token,Integer page) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if(storeId==null){
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return orderService.getOrderListBySeller(storeId,page);
    }

    /**
     * 查看订单状态
     *
     * @param token   用户鉴权token
     * @param orderId 订单id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/status")
    public GeneralResponse status(String token, Integer orderId) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if(storeId==null){
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return orderService.getStatusBySeller(storeId, orderId);
    }

    /**
     * 发货
     *
     * @param token         用户鉴权token
     * @param orderId       订单id
     * @param courierNumber 物流编号
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/deliver")
    public GeneralResponse deliver(String token, Integer orderId, String courierNumber) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if(storeId==null){
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return orderService.deliver(storeId, orderId, courierNumber);
    }

    /*处理退款*/

}
